/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.dataaccess.dao.conference.dao;

import com.nus.dataaccess.dao.base.dao.BaseDao;
import com.nus.dataaccess.dao.conference.Conference;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author kenny
 */
public class ConferenceDao extends BaseDao<Conference>{

    private static ConferenceDao instance;

    public static ConferenceDao getInstance() {
        if (null == instance) {
            instance = new ConferenceDao();
        }
        return instance;
    }
    @Override
    protected Class getReferenceClass() {return Conference.class;}

    public Conference getConferenceById(Long conferenceId){
       Criteria crit = super.createCriteria();
       crit.add(Restrictions.eq("id", conferenceId));
       crit.setMaxResults(1);
       List<Conference> conferences = crit.list();
       return (conferences!=null && !conferences.isEmpty()) ? conferences.get(0) : null;
    }

    public int getTotalCount(Map<String, Object> filters){
        Criteria crit = super.createCriteria();
        crit.setProjection(Projections.rowCount());
        
        if(filters!=null && !filters.isEmpty()){
            for(String key : filters.keySet() ){
                crit.add(Restrictions.eq(key, filters.get(key)));
            }
        }
        
        return ((Long)crit.list().get(0)).intValue();
    }

    public List<Conference> paginate(int first, int pageSize, String sortField, boolean ascending, Map<String, Object> filters){
        Criteria crit = super.createCriteria();

        if(filters!=null && !filters.isEmpty()){
            for(String key : filters.keySet() ){
                crit.add(Restrictions.eq(key, filters.get(key)));
            }
        }
        
        crit.setFirstResult(first);
        crit.setMaxResults(pageSize);
        return crit.list();
    }
}
